#ifndef __UTILE_H__
#define __UTILE_H__
#include <ctime>
#include <string>
#include <fstream>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
namespace ssslog
{
    struct Date
    {
        static size_t Now() { return time(nullptr); }
    };
    struct File
    {
        static bool Exist(const std::string& name)
        {
            std::ifstream file(name.c_str());
            return file.good();
        }
        static std::string Path(const std::string& name)
        {
            if(name.empty()) return ".";
            size_t pos = name.find_last_of("/\\");
            if(pos == std::string::npos) return ".";
            return name.substr(0,pos + 1);       
        } 
        //文件目录
        static void CreatDirectory(const std:: string& path)
        {
            if(path.empty()) return;
            if(Exist(path)) return;
            size_t pos = 0;
            size_t idx = 0;
            while (idx < path.size())
            {
                pos = path.find_first_of("/\\", idx);
                if (pos == std::string::npos)
                {
                    mkdir(path.c_str(), 0777);
                    return;
                }
                if (pos == idx)
                {
                    idx = pos + 1;
                    continue;
                }
                std::string subdir = path.substr(0, pos);
                if (subdir == "." || subdir == "..")
                {
                    idx = pos + 1;
                    continue;
                }
                if (Exist(subdir))
                {
                    idx = pos + 1;
                    continue;
                }
                mkdir(subdir.c_str(),0777);
                idx = pos + 1;
            }
        }
    };
}
#endif